<template>
  <div class="app-container">
    <el-form :model="queryParams" size="small" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="文件原名" prop="originalName">
        <el-input
          v-model="queryParams.originalName"
          placeholder="请输入文件原名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件后缀" prop="fileSuffix">
        <el-input
          v-model="queryParams.fileSuffix"
          placeholder="请输入文件后缀"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传人" prop="uploadBy">
        <el-input
          v-model="queryParams.uploadBy"
          placeholder="请输入上传人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleFile"
          v-hasPermi="['system:file:upload']"
        >上传文件
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:file:delete']"
        >批量删除
        </el-button>
      </el-form-item>
    </el-form>

    <el-table :default-sort="{prop: 'uploadTime', order: 'descending'}" v-loading="loading" :data="fileList"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="文件名" align="center" prop="filename"/>
      <el-table-column label="文件原名" align="center" prop="originalName"/>
      <el-table-column label="文件后缀" align="center" prop="fileSuffix"/>
      <el-table-column label="文件大小" align="center" prop="fileSize">
        <template slot-scope="scope">
          {{scope.row.fileSize}}
        </template>
      </el-table-column>
      <el-table-column label="文件展示" align="center" prop="url">
        <template slot-scope="scope">
          <img v-if="scope.row.fileSuffix.toLowerCase()=='jpg'||
          scope.row.fileSuffix.toLowerCase()=='png'||
          scope.row.fileSuffix.toLowerCase()=='jpeg'"
               style="width: 150px; height: 100px"
               :src="scope.row.url"
          />
          <template v-else>
            {{scope.row.url}}
          </template>
        </template>
      </el-table-column>
      <el-table-column label="上传人" align="center" prop="uploadBy"/>
      <el-table-column label="上传时间" align="center" prop="uploadTime" width="180"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDownload(scope.row)"
            v-hasPermi="['system:file:download']"
          >下载
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:file:delete']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <Pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="文件上传" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload :data="form" ref="upload" :limit="1" :headers="upload.headers" :action="upload.url"
                 :disabled="upload.isUploading"
                 :on-success="handleFileSuccess"
                 :auto-upload="false" :drag="true">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" maxlength="255"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listFile, delFile} from "@/api/system/file";
  import {getToken} from "@/utils/auth";
  import Pagination from '@/components/Pagination'

  export default {
    name: "File",
    components: {
      Pagination
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        filenames: [],
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        fileList: [],
        // 是否显示弹出层
        open: false,
        form: {
          remark: undefined
        },
        // 查询参数
        queryParams: {
          currentPage: 1,
          pageSize: 10,
          originalName: undefined,
          fileSuffix: undefined,
          uploadBy: undefined
        },
        upload: {
          open: false,
          // 设置上传的请求头部
          headers: {
            Authorization: getToken()
          },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/system/file/upload"
        },
        // 表单校验
        rules: {
          file: [
            {required: true, message: "文件不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.loading = true;
        listFile(this.queryParams).then(response => {
          this.fileList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.currentPage = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {}
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.filenames = selection.map(item => item.filename)
        this.multiple = !selection.length
      },
      /** 文件按钮操作 */
      handleFile() {
        this.form.remark = '';
        this.upload.open = true;
      },
      /** 提交按钮 */
      submitForm() {
        this.open = false;
        this.getList();
      },
      /** 下载按钮操作 */
      handleDownload(row) {
        this.download('system/file?filename=' + row.filename, {}, row.originalName)
      },
      submitFileForm() {
        this.$refs.upload.submit();
      },
      // 文件上传成功处理
      handleFileSuccess(response) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response
          .message + "</div>", "上传结果", {
          dangerouslyUseHTMLString: true
        });
        this.getList();
      },
      handleDelete(row) {
        const filenames = row.filename || this.filenames
        this.$confirm('是否确认删除所选的数据项？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delFile(filenames).then(() => {
            this.getList();
            this.$message.success("删除成功");
          })
        })
      },
    }
  };
</script>
